﻿using System.IO;
using Ionic.Zlib;

namespace Common.Utils
{
    public class CompressionUtils
    {
        /// <summary>
        /// Deflate压缩
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] DeflateByte(byte[] str)
        {
            if (str == null)
            {
                return null;
            }
            using (var output = new MemoryStream())
            {
                using (var compressor = new DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestSpeed))
                {
                    compressor.Write(str, 0, str.Length);
                }
                return output.ToArray();
            }
        }
        /// <summary>
        /// Gzip压缩
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] GZipByte(byte[] str)
        {
            if (str == null)
            {
                return null;
            }
            using (var output = new MemoryStream())
            {
                using (var compressor = new GZipStream(output, CompressionMode.Compress, CompressionLevel.BestSpeed))
                {
                    compressor.Write(str, 0, str.Length);
                }
                return output.ToArray();
            }
        }
    }
}
